<?php
require_once ('./classes/eveapi/class.api.php');
require_once ('./classes/eveapi/class.characters.php');
require_once ('./classes/eveapi/class.membertracking.php');
require_once ('./classes/eveapi/class.membertrack.php'); //  Legacy function, for testing purposes only
require_once ('./print-as-html.php');
require_once ('./config.php');
require_once ('dbconfig.php');

$api = new Api ( );
$api->setDebug ( true );
$api->setUseCache ( true ); // that's the default, done for testing purposes
$api->setTimeTolerance ( 5 ); // also the default value
$api->setCredentials ( $apiuser, $apipass );

$apicharsxml = $api->getCharacters ();
$apichars = Characters::getCharacters ( $apicharsxml );

$dbh = mysql_connect ( DB_HOST, DB_USER, DB_PASS ) or die ( mysql_error () );
mysql_select_db ( DB_NAME );

// Find the character I'm interested in


foreach ( $apichars as $index => $thischar ) {
	if ($thischar ['charname'] == $mychar) {
		$apichar = $thischar ['charid'];
	}
}
// Set Credentials
$api->setCredentials ( $apiuser, $apipass, $apichar );

$dataxml = $api->getMemberTracking ();
$data = MemberTracking::getMemberTracking ( $dataxml );

//Clear Corp Member Table
$sql = "delete  from CorpMembers";
$result = mysql_query ( $sql, $dbh ) or die ( mysql_error () );

$index = count ( $data );
foreach ( $data as $m ) {
	while ( list ( $k, $v ) = each ( $m ) ) {
		$director = "0";
		if ($k == "name") {
			if ($v == "Mograthi" || $v == "Veeja" || $v == "melgorath" || $v == "Gandain" || $v == "Bella Vick") {
				$director = "1";
			}
			$memName = mysql_real_escape_string ( $v );
			
			$sql = "insert into CorpMembers (mbrName, mbrIsDirector) values('$memName','$director')";
			if (mysql_query ( $sql, $dbh )) {
				echo "record added for $memName <br>";
			} else {
				die (mysql_error ());
			}
		
		}
	}
}

unset ( $dataxml, $data, $dataold );

?>

